package com.shaohai.live;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.ListView;

import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.reflect.TypeToken;
import com.shaohai.live.adapter.CourseAdapter;
import com.shaohai.live.entity.ApiPostTaskParams;
import com.shaohai.live.entity.Course;
import com.shaohai.live.entity.jsonResult;
import com.shaohai.live.ui.FSBarActivity;
import com.shaohai.live.ui.FSQueryBarActivity;
import com.shaohai.live.utils.AppDefine;
import com.shaohai.live.utils.RuntimeCfg;
import com.shaohai.live.utils.StringUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class WaitingCourseActivity extends FSQueryBarActivity {

    @BindView(R.id.listCourse) ListView listCourse;
    List<Course> courseList;
    CourseAdapter courseAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_waiting_course);
        ButterKnife.bind(this);
        if (RuntimeCfg.userID == 0 || StringUtils.isNullOrEmpty(RuntimeCfg.token)) {
            redirectLogin();
            return;
        }
        initializeView();
    }

    @Override
    protected void initializeView() {
        initTopBar("课程直播");
        courseList = new ArrayList<>();
        /*Course course = new Course();
        course.title = "党员干部的职业素养";
        course.courseintroduction = "党员干部的职业素养党员干部的职业素养党员干部的职业素养党员干部的职业素养";
        course.starttime = "06-18 20:00";
        course.endtime = "06-18 21:00";
        courseList.add(course);*/
        courseAdapter = new CourseAdapter(context, courseList);
        listCourse.setAdapter(courseAdapter);
        refreshRequestData();
    }

    @Override
    protected void refreshRequestData() {
        ApiPostTaskParams taskParams = new ApiPostTaskParams();
        taskParams.url = AppDefine.SERVEHOST.concat(AppDefine.COURSE_QUERY);
        taskParams.params.put("userid", RuntimeCfg.userID);
        taskParams.params.put("state", 0);
        refreshRequestDataAsync(taskParams);
    }

    @Override
    protected void onRequestDataTaskSuccess(String responseString) {
        if (responseString == null || responseString.startsWith("ERROR")) {
            showToast("登录失败，".concat(responseString + "，").concat("请稍后重试"));
        }
        try {
            jsonResult<List<Course>> result = gson.fromJson(responseString, new TypeToken<jsonResult<List<Course>>>() {
            }.getType());
            if (result != null && result.respCode.equals("200")) {
                courseList.addAll(result.data);
                courseAdapter.notifyDataSetChanged();

            } else if (result != null && !StringUtils.isNullOrEmpty(result.respMessage)) {
                showToast("暂无直播课程，".concat(result.respMessage).concat("请稍后重试"));
            } else {
                showToast("系统处理失败，校验错误");
            }
        } catch (JsonParseException ex) {
            showToast("网络连接失败，校验错误");
        }
    }
}
